package com.songlanyun.modules.ad.controller.admin;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.enums.MemberConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.exception.AdException;
import com.songlanyun.modules.user.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.ad.entity.AdLocationEntity;
import com.songlanyun.modules.ad.service.AdLocationService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;


/**
 * 广告位表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-11 13:55:19
 */
@RestController
@RequestMapping("admin/ad/adLocation")
public class AdLocationAdminController extends AbstractController {
    @Autowired
    private AdLocationService adLocationService;

    /**
     * 平台查看广告位列表
     */
    @GetMapping("/list")
//    @RequiresPermissions("ad:adLocation:list")
    public R list(@RequestParam Map<String, Object> params) {

        PageUtils page = adLocationService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 广告位下拉
     */
    @GetMapping("/select")
//    @RequiresPermissions("ad:adLocation:info")
    public R select(@RequestParam Map<String, Object> params) {
        setOwnerType(params);
        List<AdLocationEntity> list = adLocationService.select(params);

        return R.ok().put("list", list);
    }

    /**
     * 区别平台和商家
     *
     * @param params
     */
    private void setOwnerType(Map<String, Object> params) {
        if (!isShop()) {
            //平台,查询广告位类型为平台广告位的
            params.put("owner_type", MemberConstant.AdLocation.ADMIN.getCode());
        } else {
            //商家，查询广告位类型为商家广告位的
            params.put("owner_type", MemberConstant.AdLocation.SHOP.getCode());
        }
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
//    @RequiresPermissions("ad:adLocation:info")
    public R info(@PathVariable("id") Integer id) {
        AdLocationEntity adLocation = adLocationService.getById(id);

        return R.ok().put("data", adLocation);
    }

    /**
     * 保存
     */
    @SysLog("保存广告位")
    @RepeatSubmit("2")
    @PostMapping("/save")
//    @RequiresPermissions("ad:adLocation:save")
    public R save(@RequestBody AdLocationEntity adLocation) {
        ValidatorUtils.validateEntity(adLocation);
        //广告位标识码不能重复
        verifyCode(adLocation.getMarkCode());
        //只有平台才能新增
        verifyAdmin();
        adLocationService.save(adLocation);


        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改广告位")
    @RepeatSubmit("2")
    @PutMapping("/update")
//    @RequiresPermissions("ad:adLocation:update")
    public R update(@RequestBody AdLocationEntity adLocation) {
        //只有平台才能修改
        verifyAdmin();
        AdLocationEntity adLocationE = adLocationService.getById(adLocation.getId());
        if (!adLocationE.getMarkCode().equals(adLocation.getMarkCode())) {
            //广告位标识码不能重复
            verifyCode(adLocation.getMarkCode());
        }
        adLocationService.updateById(adLocation);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除广告位")
    @DeleteMapping("/delete/{id}")
//    @RequiresPermissions("ad:adLocation:delete")
    public R delete(@PathVariable("id") Integer id) {
        //只有平台才能修改
        verifyAdmin();

        adLocationService.delById(id);


        return R.ok();
    }

    /**
     * 相同广告位类型下的广告位标识码不能重复
     */
    public void verifyCode(String markCode) {

        AdLocationEntity byMarkCode = adLocationService.getByMarkCode(markCode,false);
        if (byMarkCode != null) {
            throw new RRException(AdException.AD_MARK_CODE_NOT_REPETITION);
        }
    }

}
